/**
 * related to memberList.ui
 * 
 * @Author : woon
 * @Timestamp : 2016-08-04
 */
var nf = sm("do_Notification");
var page = sm("do_Page");
var root = ui("$");
var btnSetManager = ui("btnSetManager");
var btnKick = ui("btnKick");
var circleUrl = require("url/circle");
var qqHttp = require("helper/http");
var imHelper = require("helper/im");

root.setMapping({
	"alayoutRoot.tag" : "circleId",//圈子id
	"btnSetManager.tag" : "type",//成员类型1-群主;2-管理员;3-普通成员',
	"lableMember.text" : "memberId", //成员id
	"labelName.text" : "memberName"
});

root.on("dataRefreshed", function(){
	resetBtnStatus();
});

//设置管理员
btnSetManager.on("touch",function(){
	var circleId = ui("alayoutRoot").tag;//圈子id
	var memberId = ui("lableMember").text;//成员id
	var type = ui("btnSetManager").tag;//成员类型
	
	if(type=='2'){//已经是管理员
		qqHttp.post(circleUrl.url.member.cancleCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			ui("btnSetManager").tag = 3;//变成普通成员
			resetBtnStatus();
			nf.toast("取消管理员成功");
			page.fire("reflash_member");
		});
	}else if(type=='3'){
		qqHttp.post(circleUrl.url.member.setCircleManager, {memberId:memberId,circleId:circleId}, function(data) {
			ui("btnSetManager").tag = 2;//变成管理员
			resetBtnStatus();
			nf.toast("设置管理员成功");
			page.fire("reflash_member");
		});
	}
})

function resetBtnStatus(){
	var type = ui("btnSetManager").tag;
	if(type=='1'){//圈主
		btnSetManager.hide();
	}else if(type=='2'){//已经是管理员
		btnSetManager.text = "取消管理员";
		btnSetManager.bgColor="C0C0C0FF";
	}else if(type=='3'){
		btnSetManager.text = "设为管理员";
		btnSetManager.bgColor="FF7E00FF";
	}
}

//踢走成员
btnKick.on("touch",function(){
	var circleId = ui("alayoutRoot").tag;//圈子id
	var memberId = ui("lableMember").text;//成员id
	var type = ui("btnSetManager").tag;//成员类型
	var memberName = ui("labelName").text;//成员名称
	if(type=='1'){//圈主
		nf.toast("不能踢走群主");
	}else{
		nf.confirm("你确定要踢走该成员吗?", "", "确定", "取消", function(data, e) {
			if (data == 1) {
				imHelper.sendGroupChatTip(circleId, "GroupChatTip:\""+memberName+"\" 已被移出该群聊");
				qqHttp.post(circleUrl.url.member.kickMember, {memberId:memberId,circleId:circleId}, function(data) {
					nf.toast("踢人成功");
					page.fire("kick_member");
				});
			}
		})
		
	}
})